package us.ajg0702.leaderboards.heads;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
import us.ajg0702.leaderboards.Utils.CachedData;
import us.ajg0702.leaderboards.libs.spigot.VersionSupport;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class HeadUtils {
    static HeadUtils instance;
    Map<String, CachedData<String>> skinCache = new HashMap<>();
    long lastClear = System.currentTimeMillis();
    HashMap<String, String> urlCache = new HashMap<>();
    HashMap<String, Long> urlLastget = new HashMap<>();

    public static HeadUtils getInstance() {
        if (instance == null) {
            instance = new HeadUtils();
        }
        return instance;
    }

    public static GameProfile getNonPlayerProfile(String skinURL) {
        GameProfile newSkinProfile = new GameProfile(UUID.randomUUID(), null);
        newSkinProfile.getProperties().put("textures", new Property("textures",


                Base64Coder.encodeString("{textures:{SKIN:{url:\"" + skinURL + "\"}}}")));


        return newSkinProfile;
    }

    public ItemStack getHeadItem(String name) {
        ItemStack skull = null;
        if (VersionSupport.getMinorVersion() <= 12) {
            skull = new ItemStack(Material.valueOf("SKULL_ITEM"), 1, (short) 3);
        } else if (VersionSupport.getMinorVersion() > 12) {
            skull = new ItemStack(Material.PLAYER_HEAD, 1);
        }
        String value = getHeadValue(name);
        if (value.equals("")) return skull;
        UUID hashAsId = new UUID(value.hashCode(), value.hashCode());
        return Bukkit.getUnsafe().modifyItemStack(skull, "{SkullOwner:{Id:\"" + hashAsId + "\",Properties:{textures:[{Value:\"" + value + "\"}]}}}");
    }

    public String getHeadValue(String name) {
        if ((System.currentTimeMillis() - this.lastClear) > 5400000.0D) {
            this.skinCache = new HashMap<>();
        }

        if (this.skinCache.containsKey(name) && this.skinCache.get(name).getTimeSince() < 300000.0D) {
            return (String) ((CachedData) this.skinCache.get(name)).getData();
        }

        " + name);

        Gson g = new Gson();
        JsonObject jObj = g.fromJson(result, JsonObject.class);
        if (jObj == null || jObj.get("id") == null) return "";
        String uuid = jObj.get("id").toString().replace("\"", "");
        " + uuid);
        jObj = g.fromJson(signature, JsonObject.class);
        if (jObj == null || jObj.get("id") == null) return "";
        String value = jObj.getAsJsonArray("properties").get(0).getAsJsonObject().get("value").getAsString();
        String decoded = new String(Base64.getDecoder().decode(value));
        jObj = g.fromJson(decoded, JsonObject.class);
        String skin = jObj.getAsJsonObject("textures").getAsJsonObject("SKIN").get("url").getAsString();
        byte[] skinByte = ("{\"textures\":{\"SKIN\":{\"url\":\"" + skin + "\"}}}").getBytes();
        String finalSkin = new String(Base64.getEncoder().encode(skinByte));
        this.skinCache.put(name, new CachedData(finalSkin));
        return finalSkin;
    }

    private String getURLContent(String urlStr) {
        if (this.urlLastget
                .containsKey(urlStr) && (
                System.currentTimeMillis() - this.urlLastget.get(urlStr).longValue()) < 300000.0D) {
            return this.urlCache.get(urlStr);
        }

        BufferedReader in = null;
        StringBuilder sb = new StringBuilder();

        try {
            URL url = new URL(urlStr);
            in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
            String str;
            while ((str = in.readLine()) != null) {
                sb.append(str);
            }
        } catch (Exception exception) {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException iOException) {
            }
        } finally {
            try {
                if (in != null) in.close();
            } catch (IOException iOException) {
            }
        }


        String r = sb.toString();
        this.urlCache.put(urlStr, r);
        this.urlLastget.put(urlStr, Long.valueOf(System.currentTimeMillis()));
        return r;
    }
}


*Location:D:\User\Xuan\Downloads\ajLeaderboards-1.3.3.jar\\us\ajg0702\leaderboards\heads\HeadUtils.class
 *Java compiler version:8(52.0)
         *JD-Core Version:1.1.3
